<template>
  <div>
    <el-table
      :data="tableData"
      height="300"
      border
      style="width: 100%">
      <el-table-column label="商品图片" width="200">
        <template slot-scope="scope">
          <img :src="scope.row.pimg" width="100px">
        </template>
      </el-table-column>
      <el-table-column prop="pname" label="商品名称"></el-table-column>
      <el-table-column prop="price" label="商品价格"></el-table-column>
      <el-table-column prop="count" label="商品数量"></el-table-column>
    </el-table>
    <br/>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="收货人">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="收货地址">
        <el-input v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item label="联系方式">
        <el-input v-model="form.tel"></el-input>
      </el-form-item>
    </el-form>

    <div align="right">
      <b>应付金额：<font color="red">￥{{totalPrice}}</font></b>
      <el-button type="danger" @click="submitOrder" round>提交订单</el-button>
    </div>
  </div>
</template>

<script>
    export default {
        name: "SettleAccounts",
        data(){
            return{
                tableData:JSON.parse(sessionStorage.getItem("data")).product, // 获取从购物车页面传过来的商品信息
                totalPrice:JSON.parse(sessionStorage.getItem("data")).totalPrice,
                form:{
                    name:'',
                    address:'',
                    tel:''
                }
            }
        },
        methods:{
            submitOrder(){
                var vm = this;
                // 组装数据
                var data = {
                    pro : JSON.parse(sessionStorage.getItem("pro")).userId,
                    totalPrice:this.totalPrice,
                    order_user:this.form.name,
                    order_addr: this.form.address,
                    order_tel: this.form.tel,
                    pids:[]
                }
                // 获取商品的id集合
                vm.tableData.forEach(value => {
                    data.pids.push(value.pid);
                })
                this.axios.post("http://localhost:8081/order/addOrder",this.$qs.stringify(data)).then(function (res) {
                    vm.$message.success("订单提交成功！")
                    var data={
                        totalPrice:vm.totalPrice
                    }
                    sessionStorage.setItem("data",JSON.stringify(data));
                    vm.$router.push("Pay")

                }).catch(function () {
                    vm.$message.error("订单提交失败，请联系张玉坤！")
                })
            }
        }
    }
</script>

<style scoped>

</style>

